package 力扣._100076_无限数组的最短子数组;

public class Solution {
    public int minSizeSubarray(int[] nums, int target) {
        //先计算和
        int sum = 0, len = 0;
        for (int num : nums) {
            sum += num;
        }
        len = target / sum * nums.length;
        target %= sum;
        if (target == 0)
            return len;
        //再将两段nums拼起来，看此时target的最短需多少才能拼出了
        int[] t = new int[nums.length * 2];
        for (int i = 0; i < nums.length; i++) {
            t[i] = nums[i];
            t[i + nums.length] = nums[i];
        }

        int min_len = 1000000;
        //滑动数组
        sum = 0;
        for (int i = 0, j = 0; i < t.length && j < t.length;) {
            if (sum == target) {
                min_len = Math.min(min_len, j - i);
                sum -= t[i++];
            } else if (sum < target)
                sum += t[j++];
            else
                sum -= t[i++];
        }
        if (min_len == 1000000)
            return -1;
        return len + min_len;
    }

    public static void main(String[] args) {
        int i = new Solution().minSizeSubarray(new int[]{1, 2, 3}, 5);
        System.out.println(i);
    }
}
